//
//  UIButton+BCYImage.m
//  VMall
//
//  Created by dev on 2017/7/27.
//  Copyright © 2017年 mohao. All rights reserved.
//

#import "UIButton+BCYImage.h"

@implementation UIButton(BCYImage)

-(void)setImageCenter
{
    // default 3x
    NSInteger scale = 3;
    CGSize size = CGSizeMake(self.currentImage.size.width/scale, self.currentImage.size.height/scale);
    [self setImageEdgeInsets:UIEdgeInsetsMake((self.frame.size.height - size.height)/2, (self.frame.size.width - size.width)/2, (self.frame.size.height - size.height)/2, (self.frame.size.width - size.width)/2)];
}

-(void)setImageCenterOfScale:(NSInteger)scale
{
    CGSize size = CGSizeMake(self.currentImage.size.width/scale, self.currentImage.size.height/scale);
    [self setImageEdgeInsets:UIEdgeInsetsMake((self.frame.size.height - size.height)/2, (self.frame.size.width - size.width)/2, (self.frame.size.height - size.height)/2, (self.frame.size.width - size.width)/2)];
}

-(void)setCenterByImageSize:(CGSize)size
{
    [self setImageEdgeInsets:UIEdgeInsetsMake((self.frame.size.height - size.height)/2, (self.frame.size.width - size.width)/2, (self.frame.size.height - size.height)/2, (self.frame.size.width - size.width)/2)];
}

-(CGFloat)edgeInsetLeft
{
    return self.imageEdgeInsets.left;
}

-(void)setEdgeInsetLeft:(CGFloat)edgeInsetLeft
{
    UIEdgeInsets edgeinset = self.imageEdgeInsets;
    
    CGFloat offset = edgeinset.left - edgeInsetLeft;
    edgeinset.left = edgeInsetLeft;
    edgeinset.right += offset;
    self.imageEdgeInsets = edgeinset;
}

-(CGFloat)edgeInsetRight
{
    return self.imageEdgeInsets.right;
}

-(void)setEdgeInsetRight:(CGFloat)edgeInsetRight
{
    UIEdgeInsets edgeinset = self.imageEdgeInsets;
    CGFloat offset = edgeinset.right - edgeInsetRight;
    edgeinset.right = edgeInsetRight;
    edgeinset.left += offset;
    self.imageEdgeInsets = edgeinset;
}

-(CGFloat)edgeInsetTop
{
    return self.imageEdgeInsets.top;
}

-(void)setEdgeInsetTop:(CGFloat)edgeInsetTop
{
    UIEdgeInsets edgeinset = self.imageEdgeInsets;
    CGFloat offset = edgeinset.top - edgeInsetTop;
    edgeinset.top = edgeInsetTop;
    edgeinset.bottom += offset;
    self.imageEdgeInsets = edgeinset;
}

-(CGFloat)edgeInsetBottom
{
    return self.imageEdgeInsets.bottom;
}

-(void)setEdgeInsetBottom:(CGFloat)edgeInsetBottom
{
    UIEdgeInsets edgeinset = self.imageEdgeInsets;
    CGFloat offset = edgeinset.bottom - edgeInsetBottom;
    edgeinset.bottom = edgeInsetBottom;
    edgeinset.top += offset;
    self.imageEdgeInsets = edgeinset;
}

-(CGPoint)imageCenter
{
    float centerX = self.edgeInsetLeft + (self.frame.size.width - self.edgeInsetLeft - self.edgeInsetRight)/2;
    
    float centerY = self.edgeInsetTop + (self.frame.size.height - self.edgeInsetTop - self.edgeInsetBottom)/2;
    
    return CGPointMake(centerX, centerY);
}

@end
